package com.legendshop.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BaseErrorController implements ErrorController {
    private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);

    @Override
    public String getErrorPath() {
        logger.info("出错啦！进入error页面");
        return "/common/error";
    }

    @RequestMapping("/error")
    public String error(HttpServletRequest request) {
        logger.info("出错啦！error");
        //return getErrorPath();

        //获取statusCode:401,404,500
        Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
        String errorMessage =(String)request.getAttribute("javax.servlet.error.message");
        String request_uri = (String)request.getAttribute("javax.servlet.error.request_uri");
        Throwable exception = (Throwable)request.getAttribute("javax.servlet.error.exception");
        logger.info("request_uri = {}, statusCode====={},errorMessage======{}",request_uri, statusCode,errorMessage);
        logger.error("",exception);

        if (statusCode == 404) {
            return "/common/error404";
        } else {
            return getErrorPath();
        }

    }

}
